package com.hanbang.gycatering.aboutme;

import org.json.JSONException;
import org.json.JSONObject;

import com.hanbang.gangyu.R;
import com.hanbang.gycatering.application.MyApplication;
import com.hanbang.gycatering.homepage.BaseActivity;
import com.hanbang.gycatering.property.AnnouncementDetail;
import com.hanbang.gycatering.utils.HttpInterfaces;
import com.hanbang.gycatering.utils.MyRequestCallBack;
import com.hanbang.gycatering.utils.MyToast;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class FeedBack extends BaseActivity implements OnClickListener {
	private EditText feedback_title, feedback_content;
	private String feedbacktitle, feedbackcontent;
	private ImageView feedbackre;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feedback);
		initView();

	}

	private void initView() {
		// TODO Auto-generated method stub
		((TextView) findViewById(R.id.top_bar_title)).setText("意见反馈");
		setBackArrows((ImageView) findViewById(R.id.top_bar_left_image));
		// 跳转到意见反馈列表
		  feedbackre = (ImageView) findViewById(R.id.search);
		TextView sumbit_feedback = (TextView) findViewById(R.id.sumbit_feedback);
		feedback_title = (EditText) findViewById(R.id.feedback_title);
		feedback_content = (EditText) findViewById(R.id.feedback_content);
		feedbackre.setImageResource(R.drawable.fkjl);
		feedbackre.setVisibility(View.VISIBLE);
		feedbackre.setOnClickListener(this);
		sumbit_feedback.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent();
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.search:
			intent.setClass(FeedBack.this, FeedBackRecord.class);
			startActivity(intent);
			break;
		case R.id.sumbit_feedback:
			feedbacktitle = feedback_title.getText().toString().trim();
			feedbackcontent = feedback_content.getText().toString().trim();
			if (!"".equals(feedbacktitle)) {
				if (!"".equals(feedbackcontent)) {
					upFeedBcak(feedbacktitle, feedbackcontent);
				} else {
					MyToast.show(FeedBack.this, "描述不能为空，请重新输入", 1000);
				}
			} else {
				MyToast.show(FeedBack.this, "标题不能为空，请重新输入", 1000);
			}

		default:
			break;
		}
	}

	private void upFeedBcak(String title, String content) {
		// TODO Auto-generated method stub
		((MyApplication) getApplicationContext()).httpUtils.send(
				HttpMethod.GET,
				HttpInterfaces.feedback + "&userphone="
						+ userData.getUserphone() + "&password="
						+ userData.getUserpassword() + "&title=" + title
						+ "&content=" + content, new MyRequestCallBack<String>(
						FeedBack.this, getString(R.string.loadding)) {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							if (json.getInt("state") == 1) {
								MyToast.show(FeedBack.this, "提交成功",
										Toast.LENGTH_SHORT);
								finish();
							} else {
								MyToast.show(FeedBack.this, "获取数据失败",
										Toast.LENGTH_SHORT);
							}
						} catch (JSONException e) {
							e.printStackTrace();
							MyToast.show(FeedBack.this, "获取数据失败",
									Toast.LENGTH_SHORT);
						}
					}
				});
	}
}
